package com.daydr3am.thenation;

import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import com.eightbitcloud.pagingscroller.Pager;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;

public class SubScroll extends Pager implements OnTouchListener{
	public boolean overScroll;
	boolean startsum;
	int sumOverScroll;
	protected boolean isMove;
	int movecount;
	Context con;

    public SubScroll(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
    	con=ctx;
        overScroll=false;
        this.setOnTouchListener(this);
//        contents.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 180));
    }
    boolean clamp;
    protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY){
    	super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    	clamp=clampedX;
    }
	protected boolean overScrollBy (int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, 
    	int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent){
		RootScroll main=(RootScroll)((Activity)con).findViewById(R.id.bigscroll);
	    if(clamp&&startsum){
	    	sumOverScroll+=deltaX;
	    	main.smoothScrollTo(main.getScrollX()+sumOverScroll, main.getScrollY());
	    }
    	return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, 
			maxOverScrollX, maxOverScrollY, isTouchEvent);
    	
    }
	public boolean onTouch(View v, MotionEvent event) {
		Log.v("hello", "child receive "+event.getAction());
		RootScroll main=(RootScroll)((Activity)con).findViewById(R.id.bigscroll);
		// TODO Auto-generated method stub
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			main.interupt=false;
			startsum=true;
			isMove=false;
			sumOverScroll=0;
			movecount=0;
		}
		if(event.getAction()==MotionEvent.ACTION_MOVE){
			if(movecount>5)
				isMove=true;
			else
				movecount++;
		}
		if(event.getAction()==MotionEvent.ACTION_UP){ 
			main.interupt=true;
	        int width = main.getWidth();
            int pg = (sumOverScroll+main.getScrollX() + width / 2) / width;
            Log.v("hello", "page "+main.touchPage);
            pg=main.touchPage;
            if(sumOverScroll<-20)
            	pg=main.touchPage-1;
            else if(sumOverScroll>20)
            	pg=main.touchPage+1;
            
            
	        Log.v("hello", "sumOverScroll "+sumOverScroll+" "+pg);
            main.smoothScrollTo(pg * width, 0);
            main.touchUpFinger();
		}
		return false;
	}
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev){
		Log.v("sub", "sub "+ev.getAction());
//		if(ev.getAction()==MotionEvent.ACTION_DOWN||ev.getAction()==MotionEvent.ACTION_UP)
//			return false;
//		boolean intercept=super.onInterceptTouchEvent(ev);
//		if(ev.getAction()==MotionEvent.ACTION_MOVE)
//			return true;
//		else
//			return intercept;
		return true;
	}
}
